﻿// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Mbs.CastleContainer
{
	using System;
    using Castle.Core;
    using Castle.MicroKernel;

	/// <summary>
	/// Default implementation of <see cref="IKernel"/>. 
	/// This implementation is complete and also support a kernel 
	/// hierarchy (sub containers).
	/// </summary>
	partial class MbsKernel
    {

#if !SILVERLIGHT
		public override object InitializeLifetimeService()
		{
			return null;
		}
#endif

		/// <summary>
		/// Pending
		/// </summary>
        public event HandlerDelegate HandlerRegistered;

		/// <summary>
		/// Pending
		/// </summary>
		/// <value></value>
        public event ComponentDataDelegate ComponentRegistered;

		/// <summary>
		/// Pending
		/// </summary>
		/// <value></value>
        public event ComponentDataDelegate ComponentUnregistered;

		/// <summary>
		/// Pending
		/// </summary>
		/// <value></value>
        public event ComponentInstanceDelegate ComponentCreated;

		/// <summary>
		/// Pending
		/// </summary>
		/// <value></value>
        public event ComponentInstanceDelegate ComponentDestroyed;

		/// <summary>
		/// Pending
		/// </summary>
		/// <value></value>
        public event EventHandler AddedAsChildKernel;

		/// <summary>
		/// Pending
		/// </summary>
        public event EventHandler RemovedAsChildKernel;

		/// <summary>
		/// Pending
		/// </summary>
		/// <value></value>
        public event ComponentModelDelegate ComponentModelCreated;

        public event DependencyDelegate DependencyResolving;

		protected virtual void RaiseComponentRegistered(String key, IHandler handler)
		{
            ComponentDataDelegate eventDelegate = ComponentRegistered;
			if (eventDelegate != null) eventDelegate(key, handler);

            ListnerManager.OnComponentRegistered(key, handler);
		}


		protected virtual void RaiseComponentUnregistered(String key, IHandler handler)
		{
            ComponentDataDelegate eventDelegate = ComponentUnregistered;
			if (eventDelegate != null) eventDelegate(key, handler);

            ListnerManager.OnComponentUnregistered(key, handler);
		}

		public virtual void RaiseComponentCreated(ComponentModel model, object instance)
		{
            ComponentInstanceDelegate eventDelegate = ComponentCreated;
			if (eventDelegate != null) eventDelegate(model, instance);

            ListnerManager.OnComponentCreated(model, instance);
		}

		public virtual void RaiseComponentDestroyed(ComponentModel model, object instance)
		{
            ComponentInstanceDelegate eventDelegate = ComponentDestroyed;
			if (eventDelegate != null) eventDelegate(model, instance);

            ListnerManager.OnComponentDestroyed(model, instance);
		}

		protected virtual void RaiseAddedAsChildKernel()
		{
            EventHandler eventDelegate = AddedAsChildKernel;
			if (eventDelegate != null) eventDelegate(this, EventArgs.Empty);

            ListnerManager.OnAddedAsChildKernel(this, EventArgs.Empty);
		}

		protected virtual void RaiseRemovedAsChildKernel()
		{
			EventHandler eventDelegate = RemovedAsChildKernel;
			if (eventDelegate != null) eventDelegate(this, EventArgs.Empty);

            ListnerManager.OnRemovedAsChildKernel(this, EventArgs.Empty);
		}

		protected virtual void RaiseComponentModelCreated(ComponentModel model)
		{
            ComponentModelDelegate eventDelegate = ComponentModelCreated;
            if (eventDelegate != null) eventDelegate(model);

            ListnerManager.OnComponentModelCreated(model);
		}

		public virtual void RaiseHandlerRegistered(IHandler handler)
		{
			bool stateChanged = true;

			while(stateChanged)
			{
				stateChanged = false;
                HandlerDelegate eventDelegate = HandlerRegistered;
				if (eventDelegate != null) eventDelegate(handler, ref stateChanged);

                ListnerManager.OnHandlerRegistered(handler, ref stateChanged);
			}
		}
		protected virtual void RaiseDependencyResolving(ComponentModel client, DependencyModel model, Object dependency)
		{
            DependencyDelegate eventDelegate = DependencyResolving;
			if (eventDelegate != null) eventDelegate(client, model, dependency);
		}

		#region IDeserializationCallback Members

		#endregion
	}
}
